package com.hp.qxf.canteenhou.controller;

import com.hp.qxf.canteenhou.dao.entity.Formson0819;
import com.hp.qxf.canteenhou.service.Formson0819Service;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.time.Duration;
import java.time.temporal.Temporal;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

import static com.hp.qxf.canteenhou.utils.DateUtils.*;
import static java.lang.Float.NaN;

@Controller
public class CanteenController {
    @Resource
    private Formson0819Service formson0819Service;
    @Resource
    private RedisTemplate redisTemplate;
    @RequestMapping("/select")
    public ModelAndView select(HttpServletRequest request){
        ValueOperations redis = redisTemplate.opsForValue();
        String ip = request.getRemoteAddr();
        ModelAndView modelAndView = new ModelAndView();
        List<Formson0819> canteens = formson0819Service.select();
//        Object score =  redis.get(ip);
//        if (!ObjectUtils.isEmpty(score)){
//            modelAndView.addObject("score",score);
//        }else{
//            modelAndView.addObject("score",null);
//        }
//        double v = canteens.get(0).getField0177().doubleValue() / canteens.get(0).getField0178().doubleValue();
//        if (Double.isNaN(v)){
//            v=0;
//        }
//        BigDecimal scoreNum = canteens.get(0).getField0178();
//        if (scoreNum.compareTo(new BigDecimal(0))==0){
//            modelAndView.addObject("scoreNum","今天还没有人评分哦");
//        }else{
//            modelAndView.addObject("scoreNum",scoreNum);
//        }
//        modelAndView.addObject("avgScore",String.format("%.2f",v));
        modelAndView.addObject("canteens",canteens);
        modelAndView.setViewName("canteen");
        return   modelAndView;
    }
    @RequestMapping("/giveScore")
//    @Transactional(propagation = Propagation.MANDATORY)
    public String giveScore(HttpServletRequest request, HttpSession session){
//        ValueOperations redis = redisTemplate.opsForValue();
//        String ip = request.getRemoteAddr();
//        Integer score = Integer.valueOf(request.getParameter("score"));
//        long time =(getMondayTime().getTime()-System.currentTimeMillis())/1000;
//        redis.set(ip,score,time, TimeUnit.MILLISECONDS);
//        Date date = formson0819Service.selectDay();
//        formson0819Service.getScore(score,date);
        return "redirect:/select ";
    }
}
